// pages/groupBuy/index/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 1,
        isMore: true,
        isLoading: false,
        curPage: 1,
        pageSize: 20,
        list: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            active: 1,
            isMore: true,
            isLoading: false,
            curPage: 1,
            list: []
        })
        this.getList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let {isLoading,isMore,curPage} = this.data;
        if(isLoading || !isMore){
            return;
        }
        this.setData({
            curPage: 1+curPage
        })
        this.getList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //切换tab
    changeTab: function(e) {
        let {active} = this.data;
        let v = e.detail.name;
        if(active != v){
            this.setData({
                curPage: 1,
                list: [],
                isLoading: false,
                isMore: true,
                active: v
            })
            this.getList();
        }

    },
    //获取详情
    getList: function() {
        let {active,curPage,pageSize,list} = this.data;
        this.setData({
            isLoading: true
        })
        wx.$http({
            load: true,
            method: 'get',
            url: '/business/group-goods-list',
            data: {
                status: active,
                page: curPage,
                page_size: pageSize
            }
        }).then((res) => {
            if(res.code == 1){
                let data = res.data;
                let arr = data.data;
                list = list.concat(arr);
                this.setData({
                    list: list,
                    isMore: arr.length<20?false:true
                })
            }
        }).finally(()=>{
            this.setData({
                isLoading: false
            })
        })
    },
    //添加团购
    goInfo: function (e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/groupBuy/shop/index/index?id=${id}`,
        })
    },
    //预览商品
    goReview: function (e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/groupBuy/shop/review/index?id=${id}`,
        })
    }
})